/**
 * 判断是否越界
 * @param {*} card 卡片本身
 * @param {*} dragedPosition 拖拽之后的位置
 */
export const setNewOffset = (card, mouseMoveDir = { x: 0, y: 0 }) => {
  const offsetLeft = card.offsetLeft - mouseMoveDir.x;
  const offsetTop = card.offsetTop - mouseMoveDir.y;

  return {
    x: offsetLeft < 0 ? 0 : offsetLeft,
    y: offsetTop < 0 ? 0 : offsetTop,
  };
};

/**
 * 根据内容自动扩展card高度
 * @param {*} textAreaRef
 */
export const autoGrow = (textAreaRef) => {
  const current = textAreaRef.current;
  // 设置与内容同高
  current.style.height = "auto";
  if (current.scrollHeight >= 300) {
    current.style.height = "300px";
    current.style.overflow = "auto";
  } else {
    current.style.height = current.scrollHeight + "px";
    current.style.overflow = "hidden";
  }
};

/**
 * 将选中的card层级设为最高
 * @param {*} selectedCard
 */
export const setNewZIndex = (selectedCard) => {
  selectedCard.style.zIndex = 999;

  Array.from(document.querySelectorAll(".card")).forEach((card) => {
    card.style.zIndex = selectedCard.style.zIndex - 1;
  });
};

/**
 * 获取 [min, max] 随机整数
 */
export const getRandomNumber = (min, max) => {
  return Math.trunc(Math.random() * (max + 1 - min) + min);
};
